package com.guit.hrms.service.impl;

import com.guit.hrms.mapper.SettingsMapper;
import com.guit.hrms.po.Setting;
import com.guit.hrms.service.SettingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SettingsServiceImpl implements SettingsService {
    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 获取系统设置
     * @return 设置列表
     */
    @Override
    public List<Setting> getSettings() {
        return settingsMapper.getSettings();
    }

    /**
     * 更新系统设置
     * @param setting 设置项信息
     * @return 更新结果
     */
    @Override
    public Integer updateSettings(Setting setting) {
        try {
            return settingsMapper.updateSetting(setting.getSettingId(), setting.getSettingValue());
        } catch (Exception e) {
            log.error("更新设置失败", e);
            return 0;
        }
    }

    @Override
    public boolean updateSetting(Integer settingId, String settingValue) {
        log.info("Updating setting: id={}, value={}", settingId, settingValue);
        
        if (settingId == null) {
            throw new IllegalArgumentException("设置ID不能为空");
        }
        
        try {
            int rows = settingsMapper.updateSetting(settingId, settingValue);
            log.info("Update result: {} rows affected", rows);
            return rows > 0;
        } catch (Exception e) {
            log.error("Failed to update setting", e);
            throw e;
        }
    }
}
